package com.sdc.filtros;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.sdc.bean.UsuarioBean;

public class ControleSeguranca implements Filter {
	private static final Logger log = Logger.getLogger(ControleSeguranca.class);
	HttpSession session = null;

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;

		session = request.getSession();
		// log.debug("Checando. [" + request.getRequestURL().toString() + "]");
		//

		if (request.getServletPath().contains("Servlet")
				&& !request.getServletPath().endsWith("AcessoServlet")
				&& !request.getServletPath().endsWith("ProdutosServlet")
				&& !request.getServletPath().endsWith("SacolaServlet")) {

			// verifica se tem um usu�rio na sess�o
			if (session.getAttribute("usuario") == null
					|| !((UsuarioBean) session.getAttribute("usuario"))
							.isLogado()) {

				// se n�o tiver manda
				// de volta para o
				// login

				log.debug("#Usuário precisa estar logado para acessar ["
						+ request.getRequestURL().toString() + "]");


				RequestDispatcher rd = request
						.getRequestDispatcher("/index.jsf");
				if (!response.isCommitted()) {
					rd.forward(request, response);
				}

			} else {
				chain.doFilter(request, response);
			}

		} else

		if (request.getServletPath().endsWith(".jsf")
				&& !request.getServletPath().endsWith("index.jsf")
				&& !request.getServletPath().endsWith("cad_usuario.jsf")
				&& !request.getServletPath().endsWith("error.jsf")
				&& !request.getServletPath().endsWith("sucess.jsf")
				&& !request.getServletPath().endsWith("produtos_venda.jsf")
				&& !request.getServletPath().endsWith("sacola.jsf")
				&& !request.getServletPath().endsWith("identificacao.jsf")
				&& !request.getServletPath().contains("empresa")) {

			log.debug("Filtrando [" + request.getRequestURL().toString() + "]");

			if (session.isNew()) {
				session.removeAttribute("usuario");
				response.sendRedirect(request.getContextPath() + "/index.jsf");
			}
			// verifica se tem um usu�rio na sess�o
			if (session.getAttribute("usuario") == null
					|| !((UsuarioBean) session.getAttribute("usuario"))
							.isLogado()) {

				// se n�o tiver manda
				// de volta para o
				// login

				log.debug("Usuário precisa estar logado para acessar ["
						+ request.getRequestURL().toString() + "]");
				response.sendRedirect(request.getContextPath() + "/index.jsf");
//				RequestDispatcher rd = request
//						.getRequestDispatcher("/index.jsf");
//				if (!response.isCommitted()) {
//					rd.forward(request, response);
//
//				}

			} else {
				chain.doFilter(request, response);
			}

		} else {

			chain.doFilter(request, response);

		}

	}

	public void destroy() {
	}

	public void init(FilterConfig fc) throws ServletException {

	}

}